export const API_CONFIG = {
  // 基础URL - 请替换为您的实际API地址
  BASE_URL: 'http://localhost:8089/human',
  
  // 认证相关接口
  AUTH: {
    LOGIN: '/employees/login',
    LOGOUT: '/employees/logout'
  },
  
  // 产品相关接口
  ANNOUNCEMENT: {
    LIST: '/announcements/list',
    DETAIL: '/announcements/detail'
  },
  ASSESSMENT_FILL: {
    LIST: '/assessmentFills/list',
    DETAIL: '/assessmentFills/detail',
    KPI_DETAILS: '/kpiFillDetails/list',
    SUBMIT_KPI: '/kpiFillDetails/submitKpi'
  },

  DEPARTMENT: {
    LIST: '/departments/list'
  },

  ATTENDENCE: {
    CHECK_IN: '/attendance/checkin',
    RECORD: '/attendance/checkRecord'
  }
}

// 请求封装
export interface ApiResponse<T = any> {
  code: number;
  msg: string;
  data: T;
}

// 通用的请求方法
export function request<T>(options: {
  url: string;
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
  data?: any;
  header?: any;
}): Promise<ApiResponse<T>> {
  // 获取token
  const token = wx.getStorageSync('token')
  
  return new Promise((resolve, reject) => {
    wx.request({
      url: API_CONFIG.BASE_URL + options.url,
      method: options.method || 'GET',
      data: options.data,
      header: {
        'Content-Type': 'application/json',
        ...(token ? { 'Authorization': `Bearer ${token}` } : {}),
        ...options.header
      },
      success: (res) => {
        if (res.statusCode === 200) {
          if(res.data.code == 1004) {
            // 提示登录失效
            wx.showToast({
              title: '登录失效，请重新登录',
              icon: 'none'
            })
            // 清除token
            wx.removeStorageSync('token')
            // 清除用户信息
            wx.removeStorageSync('userInfo')
            // 跳转登录页面
            wx.navigateTo({
              url: '/pages/login/login'
            })
          } else {
            resolve(res.data as ApiResponse<T>)
          }
        } else {
          reject(new Error(`请求失败: ${res.statusCode}`))
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 登录接口参数类型
export interface LoginParams {
  phone: string;
  password: string;
}

// 登录响应数据类型
export interface LoginResponse {
  token: string;
  employees: {
    id: string;
    name: string;
    phone: string;
    department: string;
    position: string;
  };
}

export const authApi = {
  // 用户登录
  async login(params: LoginParams) {
    return request<LoginResponse>({
      url: API_CONFIG.AUTH.LOGIN,
      method: 'POST',
      data: params
    })
  },
  
  // 用户登出
  async logout() {
    return request({
      url: API_CONFIG.AUTH.LOGOUT,
      method: 'POST'
    })
  }
}

export const announcementApi = {
  // 获取公告列表
  async getAnnouncementList() {
    return request({
      url: API_CONFIG.ANNOUNCEMENT.LIST,
      method: 'POST',
      data: {
        status: 'published'
      }
    })
  }
}

export const assessmentFillApi = {
  // 获取考核列表
  async getAssessmentFillList() {
    return request({
      url: API_CONFIG.ASSESSMENT_FILL.LIST,
      method: 'POST',
      data: {
        employeeId: wx.getStorageSync('userInfo').id
      }
    })
  },

  // 获取KPI详情
  async getKpiFillDetails(fillId: string) {
    return request({
      url: API_CONFIG.ASSESSMENT_FILL.KPI_DETAILS,
      method: 'POST',
      data: { fillId }
    })
  },

  // 提交KPI评分
  async submitKpiFill(data: {
    assessmentId: string;
    kpiDetails: Array<{
      kpiId: string;
      kpiName: string;
      selfScore: number;
      selfComment: string;
    }>;
  }) {
    return request({
      url: API_CONFIG.ASSESSMENT_FILL.SUBMIT_KPI,
      method: 'POST',
      data
    })
  }
}

// departments
export const departmentApi = {
  // 获取部门列表
  async getDepartmentList() {
    return request({
      url: API_CONFIG.DEPARTMENT.LIST,
      method: 'POST',
      data: {}
    })
  }
}

// save attendence
export const attendenceApi = {
  // 保存考勤
  async checkIn(data: {
    employeeId: string;
    checkDate: string;
  }) {
    return request({
      url: API_CONFIG.ATTENDENCE.CHECK_IN,
      method: 'POST',
      data
    })
  },

  // 获取考勤记录
  async checkRecord(data: {
    employeeId: string;
    checkMonth: string;
  }) {
    return request({
      url: API_CONFIG.ATTENDENCE.RECORD,
      method: 'POST',
      data
    })
  }
}
